<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/**
 * @param mprogram $mprogram
 * 
 *
 */
class client extends CI_Controller {

	public $template_data = array(
		'tmp_header'	=>	'layout/frontend/include/head',
		'tmp_right'	=>	'layout/frontend/include/right',
		'tmp_footer'	=>	'layout/frontend/include/footer',
		'tmp_banner'	=>	'layout/frontend/include/banner',
		'tmp_slider'	=>	'layout/frontend/include/slider',
		'tmp_navigation'	=>	'layout/frontend/include/navigation',
	);
	public $resource = 'RESOURCE/static/frontend/';

    function __construct() {
        parent::__construct();
		$this->load->helper('util');
		$this->load->helper('text_helper');
		$this->load->model('mcategory');
		
		$this->load->model('mpostcategory');
		$this->load->model('mpost');
		$this->load->model('mmenu');
		$this->load->model('mslider');
		$this->load->helper('util_helper');
        $this->load->model('mhtml');
		//$this->scache->html();

		$this->output->cache(120);
    }

    public function index($page = 0) {
        $data = array();
        $this->db->select('post.pos_id,post.pos_title,post.pos_code,post.pos_image,post.pos_count_view')->order_by('post.pos_id desc')->where('pos_status >',0)->limit(5);
        $data['new_post'] = $this->mpost->gets();
        $this->db->select('post.pos_id,post.pos_title,post.pos_code,post.pos_image,post.pos_count_view')->order_by('post.pos_count_view desc')->where('pos_status >',0)->limit(5);
        $data['view_posts'] = $this->mpost->gets();
        $this->db->limit(5);
        $this->db->where('sli_status >',0);
        $data['sliders'] = $this->mslider->gets();
        $data['rescode'] = 'trang-chu';

		$this->template_data['main'] = 'layout/frontend/client/index';
		$this->template->load('layout/frontend/layout',$this->template_data,$data);
    }

    public function contact() {
        $data = array();
        $this->db->select('post.pos_id,post.pos_title,post.pos_code,post.pos_image,post.pos_count_view')->order_by('post.pos_id desc')->where('pos_status >',0)->limit(10);
        $data['new_post'] = $this->mpost->gets();
        $this->db->select('post.pos_id,post.pos_title,post.pos_code,post.pos_image,post.pos_count_view')->order_by('post.pos_count_view desc')->where('pos_status >',0)->limit(10);
        $data['view_posts'] = $this->mpost->gets();
        $data['rescode'] = 'lien-he';

		$this->template_data['main'] = 'layout/frontend/client/contact';
		$this->template_data['tmp_right'] = '';
		$this->template->load('layout/frontend/layout',$this->template_data,$data);
    }

    public function category($cat_code= '', $page = 0) {  
        $data = array();
        // cate code of category
        $cat = null;
        $cat = $this->mcategory->get($cat_code, 'cat_code');
        $baseurl = null;
        $baseurl = base_url() . $cat_code;
        // If category parent
        if($cat->cat_parent == 0){
        	if($cat->cat_status <= 0){
				$query = '?q='.str_replace("-"," ",$cat_code);
				redirect(page404().$query, 301);
			}
            // set cat_id of sub category
            $subcats = null;
            $subcat_id = array();
            $this->db->where('cat_status > ', 0);
            $subcats = $this->mcategory->gets($cat->cat_id,'cat_parent');
            foreach ($subcats as $subcat){
                $subcat_id[] = $subcat->cat_id;
            }

            // paging
            $p= $this->uri->segment(2, 0);
            $page = (int)substr(strrchr($p, "-"), 1); //substr($p, -1);
            $page = ($page <= 0) ? 1 : $page;
            $config = array();
            $config['per_page'] = 10;
            $config['cur_page'] = $page;
            $config['base_url'] = base_url() . $cat_code;
            $config['total_rows'] = $this->mpost->num();
            $config['num_links'] = 5;
            $config['cur_tag_open'] = '<li class="active"><a href="#">';
			$config['cur_tag_close'] = '</a></li>';
			$config['full_tag_open'] = '<nav class="nav-pagination"><ul>';
			$config['full_tag_close'] = ' </ul><p>Trang ' . $config['cur_page'] . ' of ' . $config['total_rows'] . '</p></nav>';

	        $this->pagination->initialize($config);

	        $this->db->join('post_category', 'post_category.pos_id =' . $this->mpost->mytable . '.pos_id');
	        $this->db->where_in('cat_id', $cat->cat_id);
			$this->db->order_by('post.pos_id desc');
			$data['cat'] = @$cat;
			$data['posts'] = $this->mpost->pagination('','',$config);
			$this->db->select('post.pos_id,post.pos_title,post.pos_code,post.pos_image,post.pos_count_view')->order_by('post.pos_id desc')->where('pos_status >',0)->limit(5);
	        $data['new_post'] = $this->mpost->gets();
	        $this->db->select('post.pos_id,post.pos_title,post.pos_code,post.pos_image,post.pos_count_view')->order_by('post.pos_count_view desc')->where('pos_status >',0)->limit(5);
	        $data['view_posts'] = $this->mpost->gets();
            $this->template_data['main'] = 'layout/frontend/client/parent_cate';
			$this->template->load('layout/frontend/layout',$this->template_data,$data);
        }
        // If category sub

        if($cat->cat_parent != 0){
        	if($cat->cat_status <= 0) {
				$query = '?q='.str_replace("-"," ",$cat_code);
				redirect(page404().$query, 301);
			}
            // paging
            $p= $this->uri->segment(2, 0);
            $page = (int)substr(strrchr($p, "-"), 1); //substr($p, -1);
            $page = ($page <= 0) ? 1 : $page;
            $config = array();
            $config['per_page'] = 15;
            $config['cur_page'] = $page;
            $config['base_url'] = base_url() . $cat_code;
            $this->db->join('post_category', 'post_category.pos_id =' . $this->mpost->mytable . '.pos_id');
	        $this->db->where_in('cat_id', $cat->cat_id);
			$this->db->order_by('post.pos_id desc');
            $config['total_rows'] = $this->mpost->num();
            $config['num_links'] = 5;
            $config['cur_tag_open'] = '<li class="active"><a href="#">';
			$config['cur_tag_close'] = '</a></li>';
			$config['full_tag_open'] = '<nav class="nav-pagination"><ul>';
			$config['full_tag_close'] = ' </ul><p>Trang ' . $config['cur_page'] . ' of ' . $config['total_rows'] . '</p></nav>';

	        $this->pagination->initialize($config);

	        $this->db->join('post_category', 'post_category.pos_id =' . $this->mpost->mytable . '.pos_id');
	        $this->db->where_in('cat_id', $cat->cat_id);
			$this->db->order_by('post.pos_id desc');
			$data['cat'] = @$cat;
			$data['posts'] = $this->mpost->pagination('','',$config);
			$this->db->select('post.pos_id,post.pos_title,post.pos_code,post.pos_image,post.pos_count_view')->order_by('post.pos_id desc')->where('pos_status >',0)->limit(5);
	        $data['new_post'] = $this->mpost->gets();
	        $this->db->select('post.pos_id,post.pos_title,post.pos_code,post.pos_image,post.pos_count_view')->order_by('post.pos_count_view desc')->where('pos_status >',0)->limit(5);
	        $data['view_posts'] = $this->mpost->gets();
			$data['rescode'] = 'danh-muc';

            $this->template_data['main'] = 'layout/frontend/client/child_cate';
			$this->template->load('layout/frontend/layout',$this->template_data,$data);
        }
    }// end category() function

    // Function view()
	public function view($post_code) {
		
		$data = array();
		
		// Get and check post_id by post_code
		@$post_id = explode("-", $post_code);
		if ($post_id){
		    $post_id = (int) end($post_id);
		} 
		
		if ($post_id <= 0){
			$query = '?q='.urlencode(substr(str_replace("-"," ",$post_code),0,strlen ($query_str) - strlen(@$post_id)));
			redirect(page404().$query, 301);
		}

		// Get post
		@$post = $this->mpost->get_post($post_id);

		// Check session user
		//if (!$this->session->userdata('isAdminLogin'))
		if (empty($post) || $post->pos_status < 1 || $post->pos_date_start > time()) {
			$query = '?q='.urlencode(substr(str_replace("-"," ",$post_code),0,strlen ($query_str) - strlen(@$post_id)));
			redirect(page404().$query, 301);
		}
		// Update view in pos_count_view
		$this->mpost->update_view($post->pos_id);
		$data['post'] =  @$post;

		// Post relate
		$pos_relate = array();

		$postids = null;
		$catids = null;
		$posts = null;
		$data['post_relate'] = array();

		if ($post->pos_relate) {
		 	$pos_relate = json_decode($post->pos_relate);
		}

		if (isset($pos_relate) && $pos_relate) {
		    $posts = $this->mpost->gets_relate_post($pos_relate, $post->pos_id,6);
		    $data['post_relate'] = $posts;
		}

		if(@count($data['post_relate']) <= 6){
			$catids = $this->mpostcategory->get_category_post($post_id);
			if(!empty($catids)){
	       		$postids = $this->mpostcategory->get_post_category($catids);
	            $posts = $this->mpost->get_post_relate($postids, $post->pos_id, (6 - count($data['post_relate'])));
			    $data['post_relate'] =  @$data['post_relate'] + @$posts;
        	}	
		}

		// else{
		// 	// $posttags = $this->mpost->get_relate_post_by_tag($post->pos_id);
		// 	// if($posttags)
		// 	// 	$data['post_relate'] = $posttags;
		// 	// else{
		// 		$catids = $this->mpostcategory->get_category_post($post_id);
		// 		if(!empty($catids)){
		//        		$postids = $this->mpostcategory->get_post_category($catids);
		//             $posts = $this->mpost->get_post_relate($postids, $post->pos_id, 6);
		// 		    $data['post_relate'] = $posts;
	 //        	}	
		// 	// }
		// }

		$catids = $this->mpostcategory->get_category_post($post_id);
        if(!empty($catids)){
       		$postids = $this->mpostcategory->get_post_category($catids);
            $this->db->where('post.pos_date_start <',time())->limit(4);
            $posts = $this->mpost->get_post_other($postids, $post->pos_id);
		    $data['post_others'] = $posts;
        }
		
		// cate and subcate
		$poscate = null;
		$subcate = null;
		$poscate = $this->mpostcategory->get($post_id,'pos_id');
		
		// if (!empty($poscate)) {
		//     @$subcate = $this->mcategory->get($poscate->cat_id,'cat_id');
		    
		//     if ($subcate->cat_parent == 0) {
		//         // set catid of parent category
		//         $this->msystem->set_global('catid', $subcate->cat_id);
		//     }else{
		//         // set catid of parent category
		//         $this->msystem->set_global('catid', $subcate->cat_parent);
		//     }// end if-else
		   
		//    //set cat_id of sub category
		//     $this->msystem->set_global('cat_id', $subcate->cat_id);
		//     // post most view
		// 	$data['post_most_view'] = $this->mpost->get_view_most_post($poscate->cat_id);

		// 	// chuyen thanh muc bai viet moi nhat
		// 	//$data['post_most_view'] = $this->mpost->get_post_by_cate($poscate->cat_id, 15, 'post.pos_date_start desc');
		// 	// end muc bai viet moi nhat
		// }// end if
		
		$data['subcate'] = @$subcate;
		//$this->msystem->set_global('cat_id', @$data['subcate']->cat_id);


		
		$data['og_type'] = 'website';
		$data['canonical'] = post_url($post->pos_id,$post->pos_code);
		if ($post->pos_image)
			$data['og_image'] = base_url(). $post->pos_image;
		else 
			$data['og_image'] = get_first_img($post->pos_content);

		if($post->pos_title)
			$data['og_title'] = $post->pos_title;

		if($post->pos_snippet)
			$data['og_description'] = $post->pos_snippet;

		$this->db->select('post.pos_id,post.pos_title,post.pos_code,post.pos_image,post.pos_count_view')->order_by('post.pos_id desc')->where('pos_status >',0);
        $data['new_post'] = $this->mpost->get_post_by_cate($catids, 5, 'post.pos_id desc');
        $this->db->select('post.pos_id,post.pos_title,post.pos_code,post.pos_image,post.pos_count_view')->order_by('post.pos_count_view desc')->where('pos_status >',0);
        $data['view_posts'] = $this->mpost->get_post_by_cate($catids, 5, 'post.pos_id desc');
        $data['rescode'] = 'bai-viet';

		$this->template_data['main'] = 'layout/frontend/client/view';
		$this->template->load('layout/frontend/layout',$this->template_data,$data);

	}// end view() function
	

	function sitemap_web(){
		$this->template_data['main'] = 'layout/frontend/client/sitemap_web';
		$this->template_data['tmp_right'] = '';
		$this->template->load('layout/frontend/layout',$this->template_data,array());
	}

	// search
	function search_keyword($search_terms = '')
	{
	    // If the form has been submitted, rewrite the URL so that the search
	    // terms can be passed as a parameter to the action. Note that there
	    // are some issues with certain characters here.
	    if ($this->input->post('q'))
	    {
            $str = $this->input->post('q');
            $str_convert = Replace_TiengViet($str);
            $string = preg_replace('/[^a-zA-Z0-9_%\[().\]\\/-]/s', '', $str_convert);
            $str_lower = mb_strtolower($string);
            // $string = addslashes( stripslashes( $str ) );
	        redirect('/tim-kiem/' . $str_lower);
	       
	    }// end if

	    if($search_terms){
	        $str_search = mb_strtolower($search_terms);

	        // Determine the number of results to display per page
	        $results_per_page = 10;
	        // Establish the total number of results
	        $total_results = $this->mpost->count_search_results($str_search);
	         // paging
	        $config = array();
            $p= $this->uri->segment(3, 0);
            $page = (int)substr(strrchr($p, "-"), 1); //substr($p, -1);
            $page = ($page <= 0) ? 1 : $page;
            $config['cur_page'] = $page;
            $config['per_page'] = $results_per_page;
            $config['base_url'] = base_url() . 'tim-kiem/' . $str_search;
	        $config['total_rows'] = $total_results;
	        if($this->input->cookie('is_mobile') && $this->input->cookie('is_mobile') == 1){
	        	$config['display_pages'] = FALSE;
		        $config['first_link'] = FALSE;
		        $config['last_link'] = FALSE;	
	        }
	    	$this->pagination->initialize($config);
	        // Perform the search
	        $results = $this->mpost->search($str_search, $config);
	    }// end if
	    $data = array();
	    $data['search_terms'] = $search_terms;
	    $data['total_results'] = @$total_results;
	    $data['results'] = @$results;
	    // Render the view, passing it the necessary data;
	    // $this->db->select('post.pos_id,post.pos_title,post.pos_code,post.pos_image,post.pos_count_view')->order_by('post.pos_id desc')->where('pos_status >',0)->limit(5);
        $data['new_post'] = $this->mpost->gets();
        $this->db->select('post.pos_id,post.pos_title,post.pos_code,post.pos_image,post.pos_count_view')->order_by('post.pos_count_view desc')->where('pos_status >',0)->limit(5);
        $data['view_posts'] = $this->mpost->gets();

	    $this->template_data['main'] = 'layout/frontend/client/search';
		$this->template->load('layout/frontend/layout',$this->template_data,$data);
	}// end search_keyword()
	
	public function page404(){
		$data = array();
		$this->output->set_status_header('404');
		set_status_header(404);
		$this->db->select('post.pos_id,post.pos_title,post.pos_code,post.pos_image,post.pos_count_view')->order_by('post.pos_id desc')->where('pos_status >',0)->limit(5);
        $data['new_post'] = $this->mpost->gets();
        $this->db->select('post.pos_id,post.pos_title,post.pos_code,post.pos_image,post.pos_count_view')->order_by('post.pos_count_view desc')->where('pos_status >',0)->limit(5);
        $data['view_posts'] = $this->mpost->gets();
		$this->template_data['main'] = 'layout/frontend/include/my404';
		$this->template->load('layout/frontend/layout',$this->template_data,$data);
	}
}

